/**
 * 
 */
package com.healthshare.ui.share;

import com.healthshare.R;
import com.healthshare.busi.share.ShareListBusi;
import com.healthshare.parse.share.ShareListParse;
import com.healthshare.parse.share.ShareListParse.ShareListItem;
import com.palmedia.framework.business.BaseBusi;
import com.palmedia.framework.myinterface.UiCallBack;
import com.palmedia.framework.ui.CustomImageView;
import com.palmedia.framework.ui.CustomListView;
import com.palmedia.framework.ui.CustomListView.OnGetMoreListener;
import com.palmedia.framework.ui.CustomListView.OnRefreshListener;

import android.content.Context;
import android.util.AttributeSet;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author xudeliang
 *
 */
public class ShareView  extends LinearLayout implements UiCallBack, OnGetMoreListener, OnRefreshListener
{

	private Context context;
	//private LinearLayout buttonGroup;
	//private ArrayList<ShareListItem> shareListItems = new ArrayList<ShareListItem>();
	private CustomListView customListView;
	private ProgressBar progressBar;
	
	
	private boolean isFresh =true;
	//private NewsListBusi  nlb;
	
	
	//private int currentTabIndex=0;
	public ShareView(Context context,AttributeSet as)
	{
		super(context);
		this.context=context;
		LayoutInflater.from(context).inflate(R.layout.title_list, this, true);
		
		TextView titlecenter =(TextView) findViewById(R.id.titletextview);
		titlecenter.setText("健康/疾病/养生分享");
		
		customListView =(CustomListView) findViewById(R.id.list);
		
		customListView.setonRefreshListener(this);
		customListView.setonGetMoreListener(this);
		
		progressBar = (ProgressBar) findViewById(R.id.wait_progressBar);
		initStaticUi();
		String ts = ""+System.currentTimeMillis();
		reqShareListBusi(ts);
	}

	/**
	 * @author xudeliang
	 * 请求资讯分类数据
	 */
	private void reqShareListBusi(String starttime)
	{
		ShareListBusi slb = new ShareListBusi(this);
		slb.starttime=starttime;
		slb.iExecute();
		
		progressBar.setVisibility(View.VISIBLE);
		//ncb=null;
	}
	
	private void initStaticUi()
	{

	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.myinterface.UiCallBack#uiCallBack(com.palmedia.framework.business.BaseBusi)
	 */
	@Override
	public void uiCallBack(BaseBusi obj)
	{
		// TODO Auto-generated method stub
		progressBar.setVisibility(View.GONE);
		if(obj instanceof ShareListBusi)
		{
			ShareListParse ncp = (ShareListParse)obj.getBaseStruct();
			if(null ==ncp ||ncp.shareListItem.size()<=0)
			{
				Toast.makeText(context, "数据加载失败，请重试。", Toast.LENGTH_SHORT).show();
				return;
			}
			
			if (isFresh)// 刷新
			{
				customListView.removeAllView();
			}
			
			for(int i = 0;i < ncp.shareListItem.size();i++)
			{
				View v = buildShareRowView(ncp.shareListItem.get(i));
				if (v != null)
				{
					customListView.addViewToLast(v);
				}
			}
			
			if(isFresh)
			{
				customListView.onRefreshComplete();
			}
			else
			{
				customListView.onGetMoreComplete();
			}
			//isFresh=
		}
	}
	
	private View buildShareRowView(ShareListItem sli)
	{
		View view = View.inflate(context, R.layout.sharelistrow, null);
		

		CustomImageView head_imageView = (CustomImageView)view.findViewById(R.id.head_imageView);
		
		TextView sharenickname = (TextView)view.findViewById(R.id.sharenickname);
		
		TextView cmtnum = (TextView)view.findViewById(R.id.cmtnum);
		
		TextView colnum = (TextView)view.findViewById(R.id.colnum);
		TextView havepic = (TextView)view.findViewById(R.id.havepic);
		
		TextView timestamp = (TextView)view.findViewById(R.id.timestamp);
		
		TextView contentshare = (TextView)view.findViewById(R.id.contentshare);
		
		CustomImageView head_imagecontent = (CustomImageView)view.findViewById(R.id.head_imagecontent);
		
		//[{"contentid":"10","content":"ccc","timestamp":"1" +
		//		"326650469","nickname":"健康在线","logourl":"http:\/\/uc.jj361.com\/avatar.p" +
		//				"hp?uid=88325&size=small"
		//	,"cmtnum":"5","collectnum":"1","picurl":"","timetype":"","timevalue":""}
		
		sharenickname.setText(sli.nickname);
		cmtnum.setText("评论："+sli.cmtnum);
		colnum.setText("转发："+sli.collectnum);
		contentshare.setText(sli.content);
		sharenickname.setText(sli.nickname);
		head_imageView.setImageFromeUrl(sli.logourl);
		view.setTag(sli);
		
		return view;
	}

	/* (non-Javadoc)
	 * @see com.palmedia.framework.ui.CustomListView.OnRefreshListener#onRefresh()
	 */
	@Override
	public void onRefresh()
	{
		// TODO Auto-generated method stub
		String ts = ""+System.currentTimeMillis();
		reqShareListBusi(ts);
		this.isFresh = true;
	}

	/* (non-Javadoc)
	 * @see com.palmedia.framework.ui.CustomListView.OnGetMoreListener#onGetmore()
	 */
	@Override
	public void onGetmore()
	{
		// TODO Auto-generated method stub
		String ts = ""+System.currentTimeMillis();
		reqShareListBusi(ts);
		this.isFresh = false;
	}
	
}
